@extends('backend.layouts.base')
@section('title', '文章管理')
@section('content')
    <div class="all">
        <div id="app" v-cloak>
            <div class="vue-head">
                <div class="vue-main-title" style="margin-bottom:20px">
                    <div class="vue-main-title-left"></div>
                    <div class="vue-main-title-content">文章管理</div>
                    <div class="vue-main-title-button">
                        <el-button type="primary" plain icon="el-icon-plus" size="small" @click="addModal(0)">添加文章</el-button>
                    </div>
                </div>
                <div class="vue-search">
                    <el-form :inline="true" :model="search_form" class="demo-form-inline">
                        <el-select v-model="search_form.cid" filterable clearable placeholder="请选择分类">
                            <el-option v-for="(item,index) in category_list" :key="index" :label="item.name" :value="item.id"></el-option>
                        </el-select>
                        <el-input  v-model="search_form.title" style="width:15%;margin-left:16px;margin-right:16px;" placeholder="请输入标题关键词">
                        </el-input>
                        <el-form-item label="添加时间">
                            <el-date-picker
                                    v-model="times"
                                    type="datetimerange"
                                    value-format="yyyy-MM-dd HH:mm:ss"
                                    range-separator="至"
                                    start-placeholder="开始时间"
                                    end-placeholder="结束时间"
                                    style="margin-left:5px;"
                                    align="right">
                            </el-date-picker>
                        </el-form-item>
                        <el-form-item label="">
                            <el-button type="primary" @click="search(1)">搜索</el-button>
                        </el-form-item>
                    </el-form>
                </div>
            </div>
            <div class="vue-main">
                <div class="vue-main-form">
                    <div class="vue-main-title" style="margin-bottom:20px">
                        <div class="vue-main-title-left"></div>
                        <div class="vue-main-title-content" style="flex:0 0 130px">文章列表</div>
                        <div class="" style="text-align:left;font-size:14px;color:#999">
                            <span>文章数量：[[total]]</span>&nbsp;&nbsp;&nbsp;
                        </div>
                        <div class="vue-main-title-button">
                        </div>
                    </div>
                    <el-table :data="list" style="width: 100%">
                        <el-table-column label="ID" align="center" prop="id" width="100"></el-table-column>
                        <el-table-column label="排序" align="center" prop="display_order" width="100"></el-table-column>
                        <el-table-column label="标题" align="center" prop="title" width="100"></el-table-column>
                        <el-table-column label="分类" align="center" prop="cname"></el-table-column>
                        <el-table-column label="作者" align="center" prop="author"></el-table-column>
                        <el-table-column label="添加时间" align="center" prop="created_at"></el-table-column>
                        <el-table-column label="发布时间" align="center" prop="publish_time"></el-table-column>
                        <el-table-column prop="refund_time" label="操作" align="center" width="300">
                            <template slot-scope="scope">
                                <div class="table-option">
                                    <el-link title="编辑" :underline="false" @click="edit(scope.row)" style="text-align: center;width:30px">
                                        <a>编辑</a>
                                    </el-link>
                                    <el-link title="删除" :underline="false" @click="deleteData(scope.row.id)" style="text-align: center;width:30px">
                                        <a>删除</a>
                                    </el-link>
                                </div>
                                <div>
                                    <el-checkbox border size="mini" v-model.number="scope.row.is_top" :true-label="1" :false-label="0" @change="changeStatus(scope.row.id,'is_top')">置顶</el-checkbox>
                                    <el-checkbox border size="mini" v-model.number="scope.row.is_recommend" :true-label="1" :false-label="0" @change="changeStatus(scope.row.id,'is_recommend')">推荐</el-checkbox>
                                    <el-checkbox border size="mini" v-model.number="scope.row.is_publish" :true-label="1" :false-label="0" @change="changeStatus(scope.row.id,'is_publish')">发布</el-checkbox>
                                </div>
                            </template>
                        </el-table-column>
                    </el-table>
                </div>
            </div>
            <!-- 分页 -->
            <div class="vue-page" v-if="total>0">
                <el-row>
                    <el-col align="right">
                        <el-pagination layout="prev, pager, next,jumper" @current-change="search" :total="total"
                                       :page-size="per_page" :current-page="current_page" background
                        ></el-pagination>
                    </el-col>
                </el-row>
            </div>
        </div>
    </div>

    <script>
        var app = new Vue({
            el: "#app",
            delimiters: ['[[', ']]'],
            name: 'article',
            data() {
                return {
                    list:[],
                    search_form:{
                        title:'',
                        type:'',
                        cid:''
                    },
                    times:[],
                    category_list:[],
                    rules: {},
                    current_page:1,
                    total:1,
                    per_page:1
                }
            },
            created() {

            },
            mounted() {
                this.getCategory();
                this.getData(1);
            },
            methods: {
                getCategory() {
                    this.$http.post('{!! absolute_admin_url('content.category.query') !!}',{type:0}).then(function(response) {
                        if (response.data.result) {
                            this.category_list = response.data.data;
                        } else {
                            this.$message({
                                message: response.data.msg,
                                type: 'error'
                            });
                        }
                    }, function(response) {
                        this.$message({
                            message: response.data.msg,
                            type: 'error'
                        });
                    });
                },
                getData(page) {
                    let json = {
                        title:this.search_form.title,
                        cid:this.search_form.cid
                    };
                    if(this.times && this.times.length>0) {
                        json.start_time = this.times[0];
                        json.end_time = this.times[1];
                    }
                    let loading = this.$loading({target:document.querySelector(".content"),background: 'rgba(0, 0, 0, 0)'});
                    this.$http.post('{!! absolute_admin_url('content.article.getList') !!}',{page:page,search:json}).then(function(response) {
                        if (response.data.result) {
                            this.list = response.data.data.data;
                            this.current_page=response.data.data.current_page;
                            this.total=response.data.data.total;
                            this.per_page=response.data.data.per_page;
                            loading.close();

                        } else {
                            this.$message({
                                message: response.data.msg,
                                type: 'error'
                            });
                        }
                        loading.close();

                    }, function(response) {
                        this.$message({
                            message: response.data.msg,
                            type: 'error'
                        });
                        loading.close();
                    });
                },
                edit(item) {
                    let link = `{!! absolute_admin_url('content.article.detail') !!}`+`&id=`+item.id;
                    window.location.href = link;
                },

                search(val) {
                    this.getData(val);
                },
                // 添加
                addModal(pid) {
                    let link = `{!! absolute_admin_url('content.article.detail') !!}`;
                    if(pid){
                        link = link +`&pid=`+pid;
                    }
                    window.location.href = link;
                },
                changeStatus(id,type) {
                    let json = {id:id,type:type};
                    this.$confirm('确定修改状态吗', '提示', {confirmButtonText: '确定',cancelButtonText: '取消',type: 'warning'}).then(() => {
                        let loading = this.$loading({target:document.querySelector(".content"),background: 'rgba(0, 0, 0, 0)'});
                        this.$http.post('{!! absolute_admin_url('content.article.changeStatus') !!}',json).then(function (response) {
                                if (response.data.result) {
                                    this.$message({type: 'success',message: '操作成功!'});
                                    this.getData(this.current_page);
                                }
                                else{
                                    this.$message({type: 'error',message: response.data.msg});
                                }
                                loading.close();
                                this.search(this.current_page)
                            },function (response) {
                                this.$message({type: 'error',message: response.data.msg});
                                loading.close();
                            }
                        );
                    }).catch(() => {
                        this.$message({type: 'info',message: '已取消操作'});
                    });
                },
                deleteData(id) {
                    this.$confirm('确定删除这条数据吗', '提示', {confirmButtonText: '确定',cancelButtonText: '取消',type: 'warning'}).then(() => {
                        let loading = this.$loading({target:document.querySelector(".content"),background: 'rgba(0, 0, 0, 0)'});
                        this.$http.post('{!! absolute_admin_url('content.article.delete') !!}',{id:id}).then(function (response) {
                                if (response.data.result) {
                                    this.$message({type: 'success',message: '操作成功!'});
                                    this.getData(this.current_page);
                                }
                                else{
                                    this.$message({type: 'error',message: response.data.msg});
                                }
                                loading.close();
                                this.search(this.current_page)
                            },function (response) {
                                this.$message({type: 'error',message: response.data.msg});
                                loading.close();
                            }
                        );
                    }).catch(() => {
                        this.$message({type: 'info',message: '已取消操作'});
                    });
                },
            },
        })

    </script>
@endsection
